import { DatePicker } from '@/components/AntdProxy';
import Crud from '@/components/Crud';
import { GroupTreeSelect } from '@/components/CustomForm';
import { REQUIRED } from '@/constants/rules';
import request from '@/utils/request';
import { DownloadOutlined } from '@ant-design/icons';
import { useRequest } from 'ahooks';
import { Button } from 'antd';
import dayjs from 'dayjs';
import { useRef } from 'react';

const getList = async (params) => {
  if (!params.groupId) {
    return {
      total: 0,
      data: [],
    };
  }
  const res = await request({
    url: '/datas/dmm/operation/vehicle/summary/cost/list',
    method: 'GET',
    custom: true,
    params: params,
  });
  return {
    total: res?.total || 0,
    data: res?.data || [],
  };
};

const Index = () => {
  const crudRef = useRef();

  const { data: groupData } = useRequest(async () => {
    const res = await request({
      url: '/base/group/list',
      method: 'get',
      params: {
        index: 0,
        size: 0,
      },
    });
    return res;
  });

  const columns = [
    {
      title: '序号',
      fixed: 'left',
      width: 65,
      render: (text, record, index) => index + 1,
    },
    {
      title: '组织名称',
      dataIndex: 'groupName',
      fixed: 'left',
      width: 220,
    },
    {
      title: '车牌号',
      dataIndex: 'license',
      fixed: 'left',
      width: 120,
    },
    {
      title: '期间',
      dataIndex: 'month',
      width: 100,
      render: (text) => (text ? dayjs(text).format('YYYY-MM') : ''),
    },
    {
      title: '运行天数',
      dataIndex: 'days',
      width: 100,
    },
    {
      title: '总里程(km)',
      dataIndex: 'mileages',
      width: 120,
    },
    {
      title: '运输趟次',
      dataIndex: 'trips',
      width: 100,
    },
    {
      title: '运费收入(元)',
      dataIndex: 'taskPrice',
      width: 120,
    },
    {
      title: '平均趟次运费(元)',
      dataIndex: 'avgTaskPrice',
      width: 150,
    },
    {
      title: '长线趟次',
      dataIndex: 'mainTrips',
      width: 100,
    },
    {
      title: '长线收入(元)',
      dataIndex: 'mainTaskPrice',
      width: 120,
    },
    {
      title: '回货趟次',
      dataIndex: 'returnTrips',
      width: 100,
    },
    {
      title: '回货收入(元)',
      dataIndex: 'returnTaskPrice',
      width: 120,
    },
    {
      title: '充电费(元)',
      dataIndex: 'chargePrice',
      width: 110,
    },
    {
      title: '采暖油(元)',
      dataIndex: 'heatingPrice',
      width: 110,
    },
    {
      title: '过路费(元)',
      dataIndex: 'roadPrice',
      width: 110,
    },
    {
      title: '装卸费(元)',
      dataIndex: 'loadAndUnloadPrice',
      width: 110,
    },
    {
      title: '维修(元)',
      dataIndex: 'repairPrice',
      width: 110,
    },
    {
      title: '轮胎损耗(元)',
      dataIndex: 'tyrePrice',
      width: 120,
    },
    {
      title: '月供合计(元)',
      dataIndex: 'monthPrice',
      width: 120,
    },
    {
      title: '电池租金(元)',
      dataIndex: 'batteryRent',
      width: 120,
    },
    {
      title: '管理人员工资(元)',
      dataIndex: 'managePrice',
      width: 150,
    },
    {
      title: '司机薪酬(元)',
      dataIndex: 'driverPrice',
      width: 120,
    },
    {
      title: '驾驶员社保(元)',
      dataIndex: 'driverInsurance',
      width: 140,
    },
    {
      title: '交警罚款(元)',
      dataIndex: 'trafficTicket',
      width: 120,
    },
    {
      title: '保险费(元)',
      dataIndex: 'insurancePrice',
      width: 110,
    },
    {
      title: 'GPS费用(元)',
      dataIndex: 'gpsPrice',
      width: 120,
    },
    {
      title: '车辆年审(元)',
      dataIndex: 'auditPrice',
      width: 120,
    },
    {
      title: '税金及附加(元)',
      dataIndex: 'tax',
      width: 130,
    },
    {
      title: '其他费用(元)',
      dataIndex: 'otherPrice',
      width: 120,
    },
    {
      title: '费用总计(元)',
      dataIndex: 'totalPrice',
      width: 120,
    },
    {
      title: '营收毛利(元)',
      dataIndex: 'grossProfit',
      width: 120,
    },
    {
      title: '备注',
      dataIndex: 'remark',
      width: 200,
    },
  ];

  const fields = [
    {
      label: '组织',
      name: 'groupId',
      children: <GroupTreeSelect placeholder="请选择组织" allowClear={false} />,
      formItemProps: {
        rules: [REQUIRED],
      },
    },
    {
      label: '开始日期',
      name: 'fromTime',
      children: (
        <DatePicker
          allowClear={false}
          placeholder="请选择开始日期"
          format="YYYY-MM-DD"
        />
      ),
    },
    {
      label: '结束日期',
      name: 'toTime',
      children: (
        <DatePicker
          allowClear={false}
          placeholder="请选择结束日期"
          format="YYYY-MM-DD"
        />
      ),
    },
  ];

  const handleExport = async () => {
    const params = crudRef.current?.getParams();
    const res = await request({
      url: '/datas/dmm/operation/vehicle/summary/cost/export/list',
      method: 'GET',
      params,
    });
    if (res) {
      window.open(res);
    }
  };

  console.log('groupData?.[0]?.groupId', groupData);
  return (
    <>
      <Crud
        key={groupData?.[0]?.groupId}
        ref={crudRef}
        rowKey="vehicleId"
        queryFormProps={{
          labelCol: 7,
          initialValues: {
            fromTime: dayjs().subtract(30, 'day').startOf('day'),
            toTime: dayjs().subtract(1, 'day').endOf('day'),
            groupId: groupData?.[0]?.groupId,
          },
        }}
        fields={fields}
        columns={columns}
        request={getList}
        transformParams={(params) => {
          return {
            ...params,
            fromTime: params?.fromTime?.format('YYYY-MM-DD'),
            toTime: params?.toTime?.format('YYYY-MM-DD'),
          };
        }}
        toolbar={
          <>
            <Button
              type="primary"
              icon={<DownloadOutlined />}
              onClick={handleExport}
            >
              导出
            </Button>
          </>
        }
      />
    </>
  );
};

export default Index;
